package com.woniuxy.kaer.shop.consumer;


import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.shop.mapper.ProductMapper;
import com.woniuxy.kaer.shop.mapper.ProductOrderDataMapper;
import com.woniuxy.kaer.shop.mapper.ProductOrderMapper;
import com.woniuxy.kaer.shop.model.Product;
import com.woniuxy.kaer.shop.model.ProductOrder;
import com.woniuxy.kaer.shop.model.ProductOrderData;
import com.woniuxy.kaer.shop.service.ProductOrderService;
import com.woniuxy.kaer.user.UserInfoFeignClient;
import com.woniuxy.kaer.user.form.UpdatePointForm;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author guojiandong
 * @version 1.0
 * @CreatTime 2024-12-12 14:13
 * @description
 */
@Component
public class ConfirmPayConsumer {


    @Resource
    private AlipayClient alipayClient;

    @Resource
    private ProductOrderService productOrderService;

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private ProductOrderMapper productOrderMapper;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue("confirm_order_queue"),
                    exchange = @Exchange("confirm_pay_ex"),
                    key = "confirm_order_pay"
            )
    )
    @RabbitHandler
    public void confirmPay(String productOrderId)throws Exception {
        System.out.println("进入消费者");
//        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ productOrderId+"\"}");
//        String result = alipayClient.execute(alipayRequest).getBody();
//        String orderInfo= JSONUtil.parseObj(result).get("alipay_trade_query_response").toString();
//        String payCode=JSONUtil.parseObj(orderInfo).get("code").toString();
//        if("10000".equals(payCode)){
//            String tradeStatus = JSONUtil.parseObj(orderInfo).get("trade_status").toString();
//            if("TRADE_SUCCESS".equals(tradeStatus)){
//                String payTime = JSONUtil.parseObj(orderInfo).get("send_pay_date").toString();
//                //用户已经完成支付
//                productOrderService.updateOrder(productOrderId,payTime);
//                //更新积分
//                UpdatePointForm updatePointForm = new UpdatePointForm();
//                updatePointForm.setUserId(productOrderMapper.selectById(productOrderId).getUserId());
//                BigDecimal total = productOrderMapper.selectById(productOrderId).getTotal();
//                updatePointForm.setPointChangeNum(Integer.valueOf(total.toString()));
//                userInfoFeignClient.updatePoint(updatePointForm);
//            }
//        }else{
//            System.out.println("支付失败");
//            //表示未支付成功
//            productOrderService.cancelOrder(productOrderId);
//        }
        QueryWrapper<ProductOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_order_id",productOrderId);
        ProductOrder productOrder = productOrderMapper.selectOne(queryWrapper);
        if(productOrder==null){
            throw new RuntimeException("订单信息不存在");
        }
        if(productOrder.getPayState().equals(Data.ORDER_PAY_STATE_PAYED)){
            //更新积分
            UpdatePointForm updatePointForm = new UpdatePointForm();
            updatePointForm.setUserId(productOrderMapper.selectById(productOrderId).getUserId());
            BigDecimal total = productOrderMapper.selectById(productOrderId).getTotal();
            updatePointForm.setPointChangeNum(Integer.valueOf(total.toString()));
            userInfoFeignClient.updatePoint(updatePointForm);
        }else{
            System.out.println("支付失败");
            //表示未支付成功
            productOrderService.cancelOrder(productOrderId);
        }

    }
}
